// Operators.cpp: implementation of the Operators class.
//
//////////////////////////////////////////////////////////////////////

#include "Operators.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////



Operators::Operators()
{
	int i,j;
	if (UseOperator == TwoLevels || UseOperator==OneLevelIRE)
	{
		Initialize();
		Print();
	}
	if (UseOperator == SmallToBig )
	{
		for (i = 0; i<SIZE;i++)
			for (j=0;j<SIZE;j++)
			{
				m_op[i][j] = j;
			}
	}
	if (UseOperator == BigToSmall )
	{
		for (i = 0; i<SIZE;i++)
			for (j=SIZE-1;j>=0;j--)
			{
				m_op[i][j] = j;
			}
	}


}

Operators::~Operators()
{

}


bool Operators::Initialize()
{
	char line[1000];
	int op =0;
	int op1 = 0;
//	string line;
	ifstream myFile;
	myFile.open(OperatorsFile);
	if (myFile.is_open() == false)
		return false;
	int loc,i,j;
	for (i = 0; i<SIZE;i++)
		for (j=0;j<SIZE;j++)
		{
			m_opIRE[i][j] = 0;
			m_op[i][j] = 0;
		}
	

	//ifstream::getline(myFile, line, 1000);
	myFile.getline(line,1000);
	while (!myFile.eof())
	{
		myFile.getline(line,1000,'\t');
		for (i =0; i < SIZE; i++)
		{
			loc =0;
			myFile.getline(line,1000,'\t');
			double d=atof(line);
			
			for (int k=0;k<SIZE;k++)
			{
				int opInK = m_op[op][k]; 
				if (m_opIRE[op][opInK] <= d)
				{
					loc = k;
					break;
				}
			}

			for (int k = SIZE -1; k > loc ; k--)
				m_op[op][k] = m_op[op][k-1];
			m_op[op][loc] = i;
			m_opIRE[op][i] = d;
		}
		op++;
		
	}
	return true;
}

void Operators::Print()
{
	for (int i = 0; i< SIZE; i++)
	{
		printf("\n");
		for (int j = 0; j< SIZE; j++)
		{
			printf("%d  (%.2f)  ",m_op[i][j] + 1,m_opIRE[i][m_op[i][j]]);
		}
	}
	printf("\n");
}


int Operators::GetAt(int prev,int i)
{
	if (prev < 0) prev =0;
	if (i < 0) i =0;
	if (UseOperator==OneLevelIRE) prev = 0;

	return m_op[prev][i];
}